package com.cn.crmsystem.service.customerService.impl;

import com.cn.crmsystem.mapper.customerMapper.ContactsMapper;
import com.cn.crmsystem.pojo.Contacts;
import com.cn.crmsystem.pojo.Login;
import com.cn.crmsystem.pojo.ResultJson;
import com.cn.crmsystem.pojo.User;
import com.cn.crmsystem.service.customerService.ContactsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ContactsServiceImpl implements ContactsService {

    @Autowired
    private ContactsMapper contactsMapper;
    @Override
    public ResultJson insertContacts(Contacts contacts, Login login) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("contacts",contacts);
        map.put("login",login);
        if(contactsMapper.insertContacts(map)>0){
            return new ResultJson<>().getSuccess("添加成功");
        }else{
            return new ResultJson<>().getSuccess("添加失败");
        }
    }

    @Override
    public ResultJson deleteContacts(Integer id) {
        if(contactsMapper.deleteContacts(id)>0){
            return new ResultJson<>().getSuccess("删除成功");
        }else{
            return new ResultJson<>().getSuccess("删除失败");
        }
    }

    @Override
    public ResultJson updateContacts(Contacts contacts) {
        if(contactsMapper.updateContacts(contacts)>0){
            return new ResultJson<>().getSuccess("修改成功");
        }else{
            return new ResultJson<>().getSuccess("修改失败");
        }
    }

    @Override
    public ResultJson<List> selectContacts(Map<String, Object> map) {
        List list =contactsMapper.selectContacts(map);
        Long count=contactsMapper.selectContactsCount(map);
        if(list!=null && list.size()>0){
            return new ResultJson<>().getSuccess("查询成功",list,count);
        }else{
            return new ResultJson<>().getSuccess("查询失败");
        }
    }

    @Override
    public ResultJson deleteAllContacts(Integer[] ids) {
        int result=0;
        for(int i=0;i<ids.length;i++){
            result=contactsMapper.deleteContacts(ids[i]);
        }

        if(result>0){
            return new ResultJson<>().getSuccess("删除成功");
        }else{
            return new ResultJson<>().getSuccess("删除失败");
        }
    }
}
